﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication2.Class;

namespace WebApplication2
{
    public partial class HDMonitoring : System.Web.UI.Page
    {
        int language = 0;
        int treatmentID = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            language = (int)Session["Language"];
            treatmentID = (int)Session["treatmentID"];
            if (!Page.IsPostBack)
            {
                Deserialize_json_Object_and_Bind_GridView();
                Deserialize_json_Object_and_Bind_GridView2();
                changeIndexToWords();
                if (GridView2.Rows.Count != 0)
                {
                    populateFooter();
                }
            }
            if (GridView1.Rows.Count == 0)
            {
                Label lblDate = GridView1.Controls[0].Controls[0].FindControl("lblDate") as Label;
                Label lblTime = GridView1.Controls[0].Controls[0].FindControl("lblTime") as Label;
                Label lblBPS = GridView1.Controls[0].Controls[0].FindControl("lblBPS") as Label;
                Label lblBPD = GridView1.Controls[0].Controls[0].FindControl("lblBPD") as Label;
                Label lblPulse = GridView1.Controls[0].Controls[0].FindControl("lblPulse") as Label;
                Label lblHR = GridView1.Controls[0].Controls[0].FindControl("lblHR") as Label;
                Label lblBFR = GridView1.Controls[0].Controls[0].FindControl("lblBFR") as Label;
                Label lblBPum = GridView1.Controls[0].Controls[0].FindControl("lblBPum") as Label;
                Label lblAPR = GridView1.Controls[0].Controls[0].FindControl("lblAPR") as Label;
                Label lblVPR = GridView1.Controls[0].Controls[0].FindControl("lblVPR") as Label;
                Label lblUFR = GridView1.Controls[0].Controls[0].FindControl("lblUFR") as Label;
                Label lblUFV = GridView1.Controls[0].Controls[0].FindControl("lblUFV") as Label;
                Label lblTMP = GridView1.Controls[0].Controls[0].FindControl("lblTMP") as Label;
                Label lblFLow = GridView1.Controls[0].Controls[0].FindControl("lblFLow") as Label;
                Label lblCond = GridView1.Controls[0].Controls[0].FindControl("lblCond") as Label;
                Label lblTemp = GridView1.Controls[0].Controls[0].FindControl("lblTemp") as Label;
                Label lblKTV = GridView1.Controls[0].Controls[0].FindControl("lblKTV") as Label;
                Label lblGluc = GridView1.Controls[0].Controls[0].FindControl("lblGluc") as Label;
                Label lblSat = GridView1.Controls[0].Controls[0].FindControl("lblSat") as Label;
                Label lblHep = GridView1.Controls[0].Controls[0].FindControl("lblHep") as Label;
                Label lblFluids = GridView1.Controls[0].Controls[0].FindControl("lblFluids") as Label;

                WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                req.ContentType = "application/json";
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                StreamReader re = new StreamReader(stream);
                String json = re.ReadToEnd();
                json = "{\"FMTXHDRunLabel\":" + json + "}";

                JavaScriptSerializer ser = new JavaScriptSerializer();
                var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);
                foreach (var item in KeyWapper.fmtxhdrunlabel)
                {
                    if (item.FormLabelCode == "Tx-Sc3-Date")
                    {
                        lblDate.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Time")
                    {
                        lblTime.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-BPS")
                    {
                        lblBPS.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-BPD")
                    {
                        lblBPD.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-PR")
                    {
                        lblPulse.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-HR")
                    {
                        lblHR.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-QB")
                    {
                        lblBFR.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Bpump")
                    {
                        lblBPum.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-AP")
                    {
                        lblAPR.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-VP")
                    {
                        lblVPR.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-UFR")
                    {
                        lblUFR.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-UF")
                    {
                        lblUFV.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-TMP")
                    {
                        lblTMP.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Qd")
                    {
                        lblFLow.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Cond")
                    {
                        lblCond.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Temp")
                    {
                        lblTemp.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-KTV")
                    {
                        lblKTV.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Glu")
                    {
                        lblGluc.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-SPO2")
                    {
                        lblSat.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Hep")
                    {
                        lblHep.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Fluids")
                    {
                        lblFluids.Text = item.Language.ToString();
                    }
                }
            }
            if (GridView2.Rows.Count == 0)
            {
                Label lblDate2 = GridView2.Controls[0].Controls[0].FindControl("lblDate2") as Label;
                Label lblTime2 = GridView2.Controls[0].Controls[0].FindControl("lblTime2") as Label;
                Label lblInitials2 = GridView2.Controls[0].Controls[0].FindControl("lblInitials2") as Label;
                Label lblComp2 = GridView2.Controls[0].Controls[0].FindControl("lblComp2") as Label;
                Label lblSeverity2 = GridView2.Controls[0].Controls[0].FindControl("lblSeverity2") as Label;
                Label lblTreatmentOne2 = GridView2.Controls[0].Controls[0].FindControl("lblTreatmentOne2") as Label;
                Label lblTreatmentTwo2 = GridView2.Controls[0].Controls[0].FindControl("lblTreatmentTwo2") as Label;
                Label lblTreatmentThree2 = GridView2.Controls[0].Controls[0].FindControl("lblTreatmentThree2") as Label;
                Label lblComments2 = GridView2.Controls[0].Controls[0].FindControl("lblComments2") as Label;
                GridViewRow emptyRow = GridView2.Controls[0].Controls[0] as GridViewRow;
                DropDownList AddInitials2 = ((DropDownList)emptyRow.FindControl("AddInitials2")) as DropDownList;
                DropDownList AddComp2 = ((DropDownList)emptyRow.FindControl("AddComp2")) as DropDownList;
                DropDownList AddTreatmentOne2 = ((DropDownList)emptyRow.FindControl("AddTreatmentOne2")) as DropDownList;
                DropDownList AddTreatmentTwo2 = ((DropDownList)emptyRow.FindControl("AddTreatmentTwo2")) as DropDownList;
                DropDownList AddTreatmentThree2 = ((DropDownList)emptyRow.FindControl("AddTreatmentThree2")) as DropDownList;

                WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                req.ContentType = "application/json";
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                StreamReader re = new StreamReader(stream);
                String json = re.ReadToEnd();
                json = "{\"FMTXHDRunLabel\":" + json + "}";

                JavaScriptSerializer ser = new JavaScriptSerializer();
                var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);
                foreach (var item in KeyWapper.fmtxhdrunlabel)
                {
                    if (item.FormLabelCode == "Tx-Sc3-Date")
                    {
                        lblDate2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Rtime")
                    {
                        lblTime2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-init")
                    {
                        lblInitials2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Comev")
                    {
                        lblComp2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Srt")
                    {
                        lblSeverity2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Tr1")
                    {
                        lblTreatmentOne2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Tr2")
                    {
                        lblTreatmentTwo2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Tr3")
                    {
                        lblTreatmentThree2.Text = item.Language.ToString();
                    }

                    if (item.FormLabelCode == "Tx-Sc3-Com")
                    {
                        lblComments2.Text = item.Language.ToString();
                    }

                    if (item.User_Id != 0)
                    {
                        if (item.Initials.ToString() != "")
                        {
                            AddInitials2.Items.Add(item.Initials.ToString());
                        }
                    }

                    if (item.ComplicationsID != 0)
                    {
                        AddComp2.Items.Add(item.Complications_Language.ToString());
                    }

                    if (item.Complications_MgntID != 0)
                    {
                        AddTreatmentOne2.Items.Add(item.ComplicationMgn_Language.ToString());
                    }

                    if (item.Complications_MgntID != 0)
                    {
                        AddTreatmentTwo2.Items.Add(item.ComplicationMgn_Language.ToString());
                    }

                    if (item.Complications_MgntID != 0)
                    {
                        AddTreatmentThree2.Items.Add(item.ComplicationMgn_Language.ToString());
                    }
                }
            }
        }

        public void Deserialize_json_Object_and_Bind_GridView()
        {

           

            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRun/GetHDRunByHDTreatmentID?HDTreatmentID=19436");

            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"FMTXHDRun\":" + json + "}";

            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<MonitoringKeyWrapper>(json);
            GridView1.DataSource = keysWrapper.fmtxhdrun;
            GridView1.ShowFooter = true;
            GridView1.DataBind();
        }

        public void Deserialize_json_Object_and_Bind_GridView2()
        {
            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRun/GetHDRunComplication?HDTreatmentID=450");
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"FMTXHDRun\":" + json + "}";

            var keysWrapper = Newtonsoft.Json.JsonConvert.DeserializeObject<MonitoringKeyWrapper>(json);
            GridView2.DataSource = keysWrapper.fmtxhdrun;
            GridView2.ShowFooter = true;
            GridView2.DataBind();
        }

        public void populateFooter()
        {
            DropDownList ddlInitials1 = ((DropDownList)GridView2.FooterRow.FindControl("ddlInitials1"));
            DropDownList ddlComplications1 = ((DropDownList)GridView2.FooterRow.FindControl("ddlComplications1"));
            DropDownList ddlTreatment1 = ((DropDownList)GridView2.FooterRow.FindControl("ddlTreatment1"));
            DropDownList ddlTreatmentTwo1 = ((DropDownList)GridView2.FooterRow.FindControl("ddlTreatmentTwo1"));
            DropDownList ddlTreatmentThree1 = ((DropDownList)GridView2.FooterRow.FindControl("ddlTreatmentThree1"));

            WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
            req.ContentType = "application/json";
            WebResponse resp = req.GetResponse();
            Stream stream = resp.GetResponseStream();
            StreamReader re = new StreamReader(stream);
            String json = re.ReadToEnd();
            json = "{\"FMTXHDRunLabel\":" + json + "}";

            JavaScriptSerializer ser = new JavaScriptSerializer();
            var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

            foreach (var item in KeyWapper.fmtxhdrunlabel)
            {
                if (item.User_Id != 0)
                {
                    if (item.Initials.ToString() != "")
                    {
                        ddlInitials1.Items.Add(item.Initials.ToString());
                    }
                }

                if (item.ComplicationsID != 0)
                {
                    ddlComplications1.Items.Add(item.Complications_Language.ToString());
                }

                if (item.Complications_MgntID != 0)
                {
                    ddlTreatment1.Items.Add(item.ComplicationMgn_Language.ToString());
                }

                if (item.Complications_MgntID != 0)
                {
                    ddlTreatmentTwo1.Items.Add(item.ComplicationMgn_Language.ToString());
                }

                if (item.Complications_MgntID != 0)
                {
                    ddlTreatmentThree1.Items.Add(item.ComplicationMgn_Language.ToString());
                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            FMTXHDRun fmtxhdrun = new FMTXHDRun();
            fmtxhdrun.Time_Observed1 = ((TextBox)GridView1.FooterRow.FindControl("txtTimeObserved1")).Text;
            fmtxhdrun.Time_Observed2 = ((TextBox)GridView1.FooterRow.FindControl("txtTimeObserved2")).Text;
            string Time_Oberserved3 = fmtxhdrun.Time_Observed1 + " " + fmtxhdrun.Time_Observed2;
            fmtxhdrun.Time_Observed3 = DateTime.Parse(Time_Oberserved3);

            fmtxhdrun.BP_Sys = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtBPS")).Text);
            fmtxhdrun.BP_Dias = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtBPD")).Text);
            fmtxhdrun.Pulse = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtPulse")).Text);
            fmtxhdrun.HR = ((TextBox)GridView1.FooterRow.FindControl("txtHr")).Text;
            fmtxhdrun.Blood_Flow_Rate = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtBFR")).Text);
            fmtxhdrun.Blood_Pump_Speed = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtBPum")).Text);
            fmtxhdrun.Arterial_Pressure = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtAPR")).Text);
            fmtxhdrun.Venous_Pressure = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtVPR")).Text);
            fmtxhdrun.Ultrafiltration_Rate = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtUFR")).Text);
            fmtxhdrun.Ultrafiltration_Vol = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtUFV")).Text);
            fmtxhdrun.Transmembrane_Pressure = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtTMP")).Text);
            fmtxhdrun.Dialysate_Flow_Rate = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtFlow")).Text);
            fmtxhdrun.Conductivity = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtCond")).Text);
            fmtxhdrun.Temperature = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtTemp")).Text);
            fmtxhdrun.Kt_V = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtKTV")).Text);
            fmtxhdrun.Glucose_Value = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtGluc")).Text);
            fmtxhdrun.O2Sat = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtSat")).Text);
            fmtxhdrun.BI_MEASVOL = Convert.ToDecimal(((TextBox)GridView1.FooterRow.FindControl("txtHep")).Text);
            fmtxhdrun.Saline = Convert.ToInt32(((TextBox)GridView1.FooterRow.FindControl("txtFluids")).Text);
            fmtxhdrun.HDTreatmentID = 450;

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(fmtxhdrun);
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpClient client = new HttpClient();

            client.BaseAddress = new Uri("http://localhost:49317/api/");
            HttpResponseMessage response = client.PostAsync("FMTXHDRun/PostHDRunMon", content).Result;

           

            if (response.IsSuccessStatusCode)
            {
                Deserialize_json_Object_and_Bind_GridView();
            }
            else
            {

            }
        }

        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            FMTXHDRun fmtxhdrun = new FMTXHDRun();
            GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
            fmtxhdrun.Time_Observed1 = ((TextBox)row.FindControl("tbxTimeObserved1")).Text;
            fmtxhdrun.Time_Observed2 = ((TextBox)row.FindControl("tbxTimeObserved2")).Text;
            fmtxhdrun.BP_Sys = Convert.ToInt32(((TextBox)row.FindControl("tbxBPS")).Text);
            fmtxhdrun.BP_Dias = Convert.ToInt32(((TextBox)row.FindControl("tbxBPD")).Text);
            fmtxhdrun.Pulse = Convert.ToInt32(((TextBox)row.FindControl("tbxPulse")).Text);
            fmtxhdrun.HR = ((TextBox)row.FindControl("tbxHr")).Text;
            fmtxhdrun.Blood_Flow_Rate = Convert.ToInt32(((TextBox)row.FindControl("tbxBFR")).Text);
            fmtxhdrun.Blood_Pump_Speed = Convert.ToInt32(((TextBox)row.FindControl("tbxBPum")).Text);
            fmtxhdrun.Arterial_Pressure = Convert.ToInt32(((TextBox)row.FindControl("tbxAPR")).Text);
            fmtxhdrun.Venous_Pressure = Convert.ToInt32(((TextBox)row.FindControl("tbxVPR")).Text);
            fmtxhdrun.Ultrafiltration_Rate = Convert.ToDecimal(((TextBox)row.FindControl("tbxUFR")).Text);
            fmtxhdrun.Ultrafiltration_Vol = Convert.ToDecimal(((TextBox)row.FindControl("tbxUFV")).Text);
            fmtxhdrun.Transmembrane_Pressure = Convert.ToDecimal(((TextBox)row.FindControl("tbxTMP")).Text);
            fmtxhdrun.Dialysate_Flow_Rate = Convert.ToDecimal(((TextBox)row.FindControl("tbxFlow")).Text);
            fmtxhdrun.Conductivity = Convert.ToDecimal(((TextBox)row.FindControl("tbxCond")).Text);
            fmtxhdrun.Temperature = Convert.ToDecimal(((TextBox)row.FindControl("tbxTemp")).Text);
            fmtxhdrun.Kt_V = Convert.ToDecimal(((TextBox)row.FindControl("tbxKTV")).Text);
            fmtxhdrun.Glucose_Value = Convert.ToDecimal(((TextBox)row.FindControl("tbxGluc")).Text);
            fmtxhdrun.O2Sat = Convert.ToDecimal(((TextBox)row.FindControl("tbxSat")).Text);
            fmtxhdrun.BI_MEASVOL = Convert.ToDecimal(((TextBox)row.FindControl("tbxHep")).Text);
            fmtxhdrun.Saline = Convert.ToInt32(((TextBox)row.FindControl("tbxFluids")).Text);
            fmtxhdrun.HDTreatmentID = 450;

            string json = Newtonsoft.Json.JsonConvert.SerializeObject(fmtxhdrun);
            HttpContent content = new StringContent(json);
            content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri("http://192.168.1.156/api/");
            HttpResponseMessage response = client.PostAsync("FMTXHDRun", content).Result;
            if (response.IsSuccessStatusCode)
            {
                Deserialize_json_Object_and_Bind_GridView();
            }
            else
            {

            }
        }

        public void changeIndexToWords()
        {
            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                string InitialIndex = "";
                string complicationsIndex = "";
                string Treatment1Text = "";
                string Treatment2Text = "";
                string Treatment3Text = "";
                GridViewRow row = GridView2.Rows[i];
                Label initial = (Label)row.FindControl("lblInitials");
                Label Complications = (Label)row.FindControl("lblComplications");
                Label Treatment1 = (Label)row.FindControl("lblTreatment1");
                Label Treatment2 = (Label)row.FindControl("lblTreatment2");
                Label Treatment3 = (Label)row.FindControl("lblTreatment3");
                object initialValue = initial;
                InitialIndex = initial.Text;
                complicationsIndex = Complications.Text;
                Treatment1Text = Treatment1.Text;
                Treatment2Text = Treatment2.Text;
                Treatment3Text = Treatment3.Text;

                WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                req.ContentType = "application/json";
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                StreamReader re = new StreamReader(stream);
                String json = re.ReadToEnd();
                json = "{\"FMTXHDRunLabel\":" + json + "}";

                JavaScriptSerializer ser = new JavaScriptSerializer();
                var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                foreach (var item in KeyWapper.fmtxhdrunlabel)
                {
                    if (item.User_Id == Convert.ToInt32(InitialIndex))
                    {
                        GridView2.Rows[i].Cells[3].Text = item.Initials;
                    }

                    if (item.ComplicationsID == Convert.ToInt32(complicationsIndex))
                    {
                        GridView2.Rows[i].Cells[4].Text = item.Complications_Language;
                    }

                    if (item.Complications_MgntID == Convert.ToInt32(Treatment1Text))
                    {
                        GridView2.Rows[i].Cells[6].Text = item.ComplicationMgn_Language;
                    }

                    if (item.Complications_MgntID == Convert.ToInt32(Treatment2Text))
                    {
                        GridView2.Rows[i].Cells[7].Text = item.ComplicationMgn_Language;
                    }

                    if (item.Complications_MgntID == Convert.ToInt32(Treatment3Text))
                    {
                        GridView2.Rows[i].Cells[8].Text = item.ComplicationMgn_Language;
                    }
                }
            }
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (e.Row.Cells.Count > 0)
                {
                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"FMTXHDRunLabel\":" + json + "}";

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                    foreach (var item in KeyWapper.fmtxhdrunlabel)
                    {
                        if (item.FormLabelCode == "TxSc3-GpMon")
                        {
                            lblMonitoring.Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Date")
                        {
                            e.Row.Cells[1].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Time")
                        {
                            e.Row.Cells[2].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-BPS")
                        {
                            e.Row.Cells[3].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-BPD")
                        {
                            e.Row.Cells[4].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-PR")
                        {
                            e.Row.Cells[5].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-HR")
                        {
                            e.Row.Cells[6].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-QB")
                        {
                            e.Row.Cells[7].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Bpump")
                        {
                            e.Row.Cells[8].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-AP")
                        {
                            e.Row.Cells[9].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-VP")
                        {
                            e.Row.Cells[10].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-UFR")
                        {
                            e.Row.Cells[11].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-UF")
                        {
                            e.Row.Cells[12].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-TMP")
                        {
                            e.Row.Cells[13].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Qd")
                        {
                            e.Row.Cells[14].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Cond")
                        {
                            e.Row.Cells[15].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Temp")
                        {
                            e.Row.Cells[16].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-KTV")
                        {
                            e.Row.Cells[17].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Glu")
                        {
                            e.Row.Cells[18].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-SPO2")
                        {
                            e.Row.Cells[19].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Hep")
                        {
                            e.Row.Cells[20].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Fluids")
                        {
                            e.Row.Cells[21].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "TxSc3-GpCompEv")
                        {
                            lblEvents.Text = item.Language.ToString();
                        }
                    }
                }
            }
        }

        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView1.EditIndex = -1;
            Deserialize_json_Object_and_Bind_GridView();
        }

        protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
        {
            GridView1.EditIndex = e.NewEditIndex;
            Deserialize_json_Object_and_Bind_GridView();
        }

        protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow
           && e.Row.RowIndex == GridView2.EditIndex)
            {
                WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                req.ContentType = "application/json";
                WebResponse resp = req.GetResponse();
                Stream stream = resp.GetResponseStream();
                StreamReader re = new StreamReader(stream);
                String json = re.ReadToEnd();
                json = "{\"FMTXHDRunLabel\":" + json + "}";

                JavaScriptSerializer ser = new JavaScriptSerializer();
                var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                foreach (var item in KeyWapper.fmtxhdrunlabel)
                {
                    if (item.User_Id != 0)
                    {
                        DropDownList ddlInitials = e.Row.FindControl("ddlInitials") as DropDownList;
                        if (item.Initials.ToString() != "")
                        {
                            ddlInitials.Items.Add(item.Initials.ToString());
                        }
                    }

                    if (item.ComplicationsID != 0)
                    {
                        DropDownList ddlComplications = e.Row.FindControl("ddlComplications") as DropDownList;
                        ddlComplications.Items.Add(item.Complications_Language.ToString());
                    }

                    if (item.Complications_MgntID != 0)
                    {
                        DropDownList ddlTreatment = e.Row.FindControl("ddlTreatment") as DropDownList;
                        ddlTreatment.Items.Add(item.ComplicationMgn_Language.ToString());
                    }

                    if (item.Complications_MgntID != 0)
                    {
                        DropDownList ddlTreatmentTwo = e.Row.FindControl("ddlTreatmentTwo") as DropDownList;
                        ddlTreatmentTwo.Items.Add(item.ComplicationMgn_Language.ToString());
                    }

                    if (item.Complications_MgntID != 0)
                    {
                        DropDownList ddlTreatmentThree = e.Row.FindControl("ddlTreatmentThree") as DropDownList;
                        ddlTreatmentThree.Items.Add(item.ComplicationMgn_Language.ToString());
                    }
                }

            }
            if (e.Row.RowType == DataControlRowType.Header)
            {
                if (e.Row.Cells.Count > 0)
                {
                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"FMTXHDRunLabel\":" + json + "}";

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                    foreach (var item in KeyWapper.fmtxhdrunlabel)
                    {
                        if (item.FormLabelCode == "Tx-Sc3-Date")
                        {
                            e.Row.Cells[1].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Rtime")
                        {
                            e.Row.Cells[2].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-init")
                        {
                            e.Row.Cells[3].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Comev")
                        {
                            e.Row.Cells[4].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Srt")
                        {
                            e.Row.Cells[5].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Tr1")
                        {
                            e.Row.Cells[6].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Tr2")
                        {
                            e.Row.Cells[7].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Tr3")
                        {
                            e.Row.Cells[8].Text = item.Language.ToString();
                        }

                        if (item.FormLabelCode == "Tx-Sc3-Com")
                        {
                            e.Row.Cells[9].Text = item.Language.ToString();
                        }
                    }
                }
            }
        }

        protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridView2.EditIndex = e.NewEditIndex;
            Deserialize_json_Object_and_Bind_GridView2();
            DropDownList dd = (DropDownList)GridView2.FindControl("ddlTreatmentThree");

            for (int i = 0; i < GridView2.Rows.Count; i++)
            {
                if (i != GridView2.EditIndex)
                {
                    string InitialIndex = "";
                    string complicationsIndex = "";
                    string Treatment1Text = "";
                    string Treatment2Text = "";
                    string Treatment3Text = "";
                    GridViewRow row = GridView2.Rows[i];
                    Label initial = (Label)row.FindControl("lblInitials");
                    Label Complications = (Label)row.FindControl("lblComplications");
                    Label Treatment1 = (Label)row.FindControl("lblTreatment1");
                    Label Treatment2 = (Label)row.FindControl("lblTreatment2");
                    Label Treatment3 = (Label)row.FindControl("lblTreatment3");
                    object initialValue = initial;
                    InitialIndex = initial.Text;
                    complicationsIndex = Complications.Text;
                    Treatment1Text = Treatment1.Text;
                    Treatment2Text = Treatment2.Text;
                    Treatment3Text = Treatment3.Text;

                    WebRequest req = WebRequest.Create("http://192.168.1.156/api/FMTXHDRunLabel?LanguageID=" + language);
                    req.ContentType = "application/json";
                    WebResponse resp = req.GetResponse();
                    Stream stream = resp.GetResponseStream();
                    StreamReader re = new StreamReader(stream);
                    String json = re.ReadToEnd();
                    json = "{\"FMTXHDRunLabel\":" + json + "}";

                    JavaScriptSerializer ser = new JavaScriptSerializer();
                    var KeyWapper = Newtonsoft.Json.JsonConvert.DeserializeObject<HDRunKeyWrapper>(json);

                    foreach (var item in KeyWapper.fmtxhdrunlabel)
                    {
                        if (item.User_Id == Convert.ToInt32(InitialIndex))
                        {
                            GridView2.Rows[i].Cells[3].Text = item.Initials;
                        }

                        if (item.ComplicationsID == Convert.ToInt32(complicationsIndex))
                        {
                            GridView2.Rows[i].Cells[4].Text = item.Complications_Language;
                        }

                        if (item.Complications_MgntID == Convert.ToInt32(Treatment1Text))
                        {
                            GridView2.Rows[i].Cells[6].Text = item.ComplicationMgn_Language;
                        }

                        if (item.Complications_MgntID == Convert.ToInt32(Treatment2Text))
                        {
                            GridView2.Rows[i].Cells[7].Text = item.ComplicationMgn_Language;
                        }

                        if (item.Complications_MgntID == Convert.ToInt32(Treatment3Text))
                        {
                            GridView2.Rows[i].Cells[8].Text = item.ComplicationMgn_Language;
                        }
                    }
                }
            }
        }

        protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridView2.EditIndex = -1;
            Deserialize_json_Object_and_Bind_GridView2();
            changeIndexToWords();
            populateFooter();
        }

    }
}